const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    snNumber: "",
    name: ""
  },

  onShow() {
    wx.getStorage({
      key: "snNumber",
      success: (res) => {
        const { snNumber } = res.data;
        this.setData({
          snNumber
        });
      }
    });
  },

  toAddDevice() {
    if (!this.data.snNumber) {
      return wx.showToast({
        title: "请输入设备编号",
        icon: "error",
        duration: 2000
      });
    }
    this.addBindDevice();
  },

  addBindDevice() {
    let params = {
      sort: 1,
      name: this.data.name,
      snNumber: this.data.snNumber,
      companyId: app.globalData.userInfo.companyData.companyId
    };
    wx.$apis.addBindDevice(params).then((res) => {
      wx.showToast({
        title: "添加成功",
        icon: "success",
        duration: 2000
      }).then(() => {
        setTimeout(() => {
          wx.navigateBack();
        }, 1000);
      });
    });
  },

  // 跳转扫描页面
  toScanning() {
    wx.navigateTo({
      url: "/pages/management/scanning/index"
    });
  }
});
